﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		BlogTheme.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Web;
using Dexter.CallContext;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	The concrete implementation of IBlogTheme
	/// </summary>
	public class BlogTheme : IBlogTheme {
		readonly HttpContextBase context;
		readonly string themeRoot;
		readonly IUrlBuilderBase urlBuilderRoot;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "BlogTheme" /> class.
		/// </summary>
		/// <param name = "themeName">Name of the theme.</param>
		/// <param name = "urlBuilderRoot">The URL builder root.</param>
		/// <param name = "context">The context.</param>
		public BlogTheme ( string themeName , IUrlBuilderBase urlBuilderRoot , IDexterContext context ) {
			if ( themeName.IsNullOrEmpty ( ) ) {
				throw new ArgumentException ( "Invalid blog theme themeName." , "themeName" );
			}

			this.urlBuilderRoot = urlBuilderRoot;
			this.context = context.Context;

			Name = themeName;
			themeRoot = string.Format ( "/Themes/{0}" , themeName );
		}

		/// <summary>
		/// 	Gets the themeName.
		/// </summary>
		/// <value>The themeName.</value>
		public virtual string Name { get; private set; }

		/// <summary>
		/// 	Gets the image URL.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public Uri GetFileUrl ( string image ) {
			return new Uri ( urlBuilderRoot.HomePage, string.Format ( "{0}/Contents/{1}", themeRoot, image ) );
		}

		/// <summary>
		/// 	Gets the image path.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public string GetFilePath ( string image ) {
			return context.Request.MapPath ( string.Format ( "~/{0}/Contents/{1}", themeRoot, image ) );
		}

		/// <summary>
		/// 	Gets the image URL.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public virtual Uri GetImageUrl ( string image ) {
			return new Uri ( urlBuilderRoot.HomePage , string.Format ( "{0}/Images/{1}" , themeRoot , image ) );
		}

		/// <summary>
		/// 	Gets the image path.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public virtual string GetImagePath ( string image ) {
			return context.Request.MapPath ( string.Format ( "~/{0}/Images/{1}" , themeRoot , image ) );
		}

		/// <summary>
		/// 	Gets the script URL.
		/// </summary>
		/// <param name = "name">The name.</param>
		/// <returns></returns>
		public virtual Uri GetScriptUrl ( string name ) {
			var relativeUri = string.Format ( "{0}/Scripts/{1}" , themeRoot , name );
			return new Uri ( urlBuilderRoot.HomePage , relativeUri );
		}

		/// <summary>
		/// 	Gets the script path.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public virtual string GetScriptPath ( string image ) {
			return context.Request.MapPath ( string.Format ( "~/{0}/Scripts/{1}" , themeRoot , image ) );
		}

		/// <summary>
		/// 	Gets the style URL.
		/// </summary>
		/// <param name = "name">The name.</param>
		/// <returns></returns>
		public virtual Uri GetStyleUrl ( string name ) {
			return new Uri ( urlBuilderRoot.HomePage , string.Format ( "{0}/Styles/{1}" , themeRoot , name ) );
		}

		/// <summary>
		/// 	Gets the style path.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public virtual string GetStylePath ( string image ) {
			return context.Request.MapPath ( string.Format ( "~/{0}/Styles/{1}" , themeRoot , image ) );
		}

		/// <summary>
		/// 	Gets the media URL.
		/// </summary>
		/// <param name = "name">The name.</param>
		/// <returns></returns>
		public virtual Uri GetMediaUrl ( string name ) {
			return new Uri ( urlBuilderRoot.HomePage , string.Format ( "{0}/Media/{1}" , themeRoot , name ) );
		}

		/// <summary>
		/// 	Gets the media path.
		/// </summary>
		/// <param name = "image">The image.</param>
		/// <returns></returns>
		public virtual string GetMediaPath ( string image ) {
			return context.Request.MapPath ( string.Format ( "~/{0}/Media/{1}" , themeRoot , image ) );
		}

		/// <summary>
		/// 	Gets the masterpage URL.
		/// </summary>
		/// <returns></returns>
		public virtual Uri GetMasterpageUrl ( ) {
			return new Uri ( urlBuilderRoot.HomePage , string.Format ( "{0}/{1}" , themeRoot , "Views/Shared/Site.Master" ) );
		}

		/// <summary>
		/// 	Templates the file path.
		/// </summary>
		/// <param name = "templateFileName">Name of the template file.</param>
		/// <returns></returns>
		public virtual string TemplateFilePath ( string templateFileName ) {
			return string.Format ( "{0}Themes\\{1}\\Templates\\Notification\\{2}.dxt" , AppDomain.CurrentDomain.BaseDirectory , this.Name , templateFileName );
		}

		/// <summary>
		/// Templates the master file path.
		/// </summary>
		/// <param name = "masterFileName">Name of the master file.</param>
		/// <returns></returns>
		public string TemplateMasterFilePath ( string masterFileName ) {
			return string.Format ( "{0}Themes\\{1}\\Templates\\Master\\{2}.dxt", AppDomain.CurrentDomain.BaseDirectory, this.Name, masterFileName );
		}
	}
}
